package com.colorful.insuranceproject.commonActivity;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.bean.VerifyBean;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.request.UserRequest;
import com.lovely3x.common.utils.Response;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/8/16.
 * <p>
 * 设置密码
 */
public class SetPasswordActivity extends TitleActivity implements TextWatcher {

    private static final int SET_PWD_URL = 0X1;

    public static final String EXTRA_MESSAGE_ID = "extra_message_id";

    private UserRequest userRequest;

    @Bind(R.id.tv_pwd)
    EditText tvPwd;

    @Bind(R.id.tv_pwd_again)
    EditText tvPwdAgain;

    @Bind(R.id.img_show_one)
    ImageView imgShowOne;

    @Bind(R.id.img_show_two)
    ImageView imgShowTwo;

    @Bind(R.id.tv_set)
    TextView tvSet;

    private int judgeIconOld;

    private int judgeIconNew;

    VerifyBean vb;

    @Override
    protected int getContentView() {
        return R.layout.activity_set_pwd;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.set_pwd));
    }

    @Override
    protected void onViewInitialized() {
        judgeIconOld = 0;
        judgeIconNew = 0;
        userRequest = new UserRequest(getHandler());
        tvPwdAgain.addTextChangedListener(this);
    }

    @Override
    protected void onInitExtras(@NonNull Bundle bundle) {
        super.onInitExtras(bundle);
        vb = bundle.getParcelable(EXTRA_MESSAGE_ID);
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case SET_PWD_URL:
                if (response.isSuccessful) {
                    showToast(getString(R.string.set_pwd_success));
                    setResult(RESULT_OK);
                    finish();
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    /**
     * 页面监听处理
     */
    @OnClick({R.id.img_show_one, R.id.img_show_two, R.id.tv_set})
    void OnClicked(View view) {
        switch (view.getId()) {
            case R.id.img_show_one:
                if (judgeIconOld == 0) {
                    tvPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    judgeIconOld = 1;
                    imgShowOne.setImageResource(R.drawable.icon_bukejian);
                } else {
                    tvPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    judgeIconOld = 0;
                    imgShowOne.setImageResource(R.drawable.icon_kejian);
                }
                if (tvPwd.length() > 1) {
                    tvPwd.setSelection(tvPwd.length());
                }
                break;
            case R.id.img_show_two:
                if (judgeIconNew == 0) {
                    tvPwdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    judgeIconNew = 1;
                    imgShowTwo.setImageResource(R.drawable.icon_bukejian);
                } else {
                    tvPwdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    judgeIconNew = 0;
                    imgShowTwo.setImageResource(R.drawable.icon_kejian);
                }
                if (tvPwdAgain.length() > 1) {
                    tvPwdAgain.setSelection(tvPwdAgain.length());
                }
                break;
            case R.id.tv_set:
                String pwd1 = tvPwd.getText().toString().trim();
                String pwd2 = tvPwdAgain.getText().toString().trim();
                if (pwd1.length() < 6 || pwd1.length() > 20) {
                    showToast(getString(R.string.please_enter_a_password_of_bits_in_length));
                } else if (!pwd1.equals(pwd2)) {
                    showToast(getString(R.string.the_two_password_input_is_inconsistent));
                } else {
                    userRequest.setPasswordUrl(pwd1, vb.getMessageId(), vb.getResult(),vb.getCode(), SET_PWD_URL);
                }
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        //  ignore
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String pwd1 = tvPwd.getText().toString().trim();
        String pwd2 = tvPwdAgain.getText().toString().trim();
        if (s.length() == 0) {
            tvSet.setBackground(ContextCompat.getDrawable(mActivity, R.drawable.btn_gray_such_as_login));
        } else if (s.length() > 6 || s.length() == 20) {
            if (!pwd1.equals(pwd2)) {
                tvSet.setBackground(ContextCompat.getDrawable(mActivity, R.drawable.btn_gray_such_as_login));
            } else {
                tvSet.setBackground(ContextCompat.getDrawable(mActivity, R.drawable.btn_blue_such_as_login));
            }
        }
    }
}
